#include <linux/module.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <asm/io.h>
#include <linux/semaphore.h>
#include <linux/gpio.h>
#include <mach/hardware.h>

#include <mach/regs-gpio.h>
/* #include <mach/gpio-samsung.h> */
#include <plat/gpio-cfg.h>

#include "sccb.h"
#include "s3c2440_camif.h"

#define OV9650_SCCB_ADDR    0x60
#define OV9650_MANUFACT_ID    0x7FA2
#define OV9650_PRODUCT_ID    0x9650

static struct ov9650_reg
{
    u8 subaddr;
    u8 value;
}regs[] = {
    /* OV9650 intialization parameter table for SXGA application */
    {0x12, 0x80}, {0x39, 0x43}, {0x38, 0x12}, {0x37, 0x00}, {0x0e, 0x20},
    {0x1e, 0x1c}, {0x01, 0x80}, {0x02, 0x80}, {0x00, 0x00}, {0x10, 0xf0},
    {0x04, 0x00}, {0x0c, 0x00}, {0x0d, 0x00}, {0x11, 0x80}, {0x12, 0x00},
    {0x14, 0x2e}, {0x15, 0x00}, {0x18, 0xbd}, {0x17, 0x1d}, {0x32, 0xbf},
    {0x03, 0x12}, {0x1a, 0x81}, {0x19, 0x01}, {0x3f, 0xa6}, {0x41, 0x02},
    {0x42, 0x08}, {0x1b, 0x00}, {0x16, 0x06}, {0x33, 0xc0}, {0x34, 0xbf},
    {0xa8, 0x80}, {0x96, 0x04}, {0x3a, 0x00}, {0x8e, 0x00}, {0x3c, 0x77},
    {0x8b, 0x06}, {0x35, 0x91}, {0x94, 0x88}, {0x95, 0x88}, {0x40, 0xc1},
    {0x29, 0x3f}, {0x0f, 0x42}, {0x13, 0xe5}, {0x3d, 0x92}, {0x69, 0x80},
    {0x5c, 0x96}, {0x5d, 0x96}, {0x5e, 0x10}, {0x59, 0xeb}, {0x5a, 0x9c},
    {0x5b, 0x55}, {0x43, 0xf0}, {0x44, 0x10}, {0x45, 0x55}, {0x46, 0x86},
    {0x47, 0x64}, {0x48, 0x86}, {0x5f, 0xe0}, {0x60, 0x8c}, {0x61, 0x20},
    {0xa5, 0xd9}, {0xa4, 0x74}, {0x8d, 0x02}, {0x13, 0xe7}, {0x4f, 0x3a},
    {0x50, 0x3d}, {0x51, 0x03}, {0x52, 0x12}, {0x53, 0x26}, {0x54, 0x38},
    {0x55, 0x40}, {0x56, 0x40}, {0x57, 0x40}, {0x58, 0x0d}, {0x8c, 0x23},
    {0x3e, 0x02}, {0xa9, 0xb8}, {0xaa, 0x92}, {0xab, 0x0a}, {0x8f, 0xdf},
    {0x90, 0x00}, {0x91, 0x00}, {0x9f, 0x00}, {0x3a, 0x0c}, {0x24, 0x70},
    {0x25, 0x64}, {0x26, 0xc3}, {0x2a, 0x12}, {0x2b, 0x46}, {0x3b, 0x19},
    {0x6c, 0x40}, {0x6d, 0x30}, {0x6e, 0x4b}, {0x6f, 0x60},
    {0x70, 0x70}, {0x71, 0x70}, {0x72, 0x70}, {0x73, 0x70},
    {0x74, 0x60}, {0x75, 0x60}, {0x76, 0x50}, {0x77, 0x48},
    {0x78, 0x3a}, {0x79, 0x2e}, {0x7a, 0x28}, {0x7b, 0x22},
    {0x7c, 0x04}, {0x7d, 0x07}, {0x7e, 0x10}, {0x7f, 0x28},
    {0x80, 0x36}, {0x81, 0x44}, {0x82, 0x52}, {0x83, 0x60},
    {0x84, 0x6c}, {0x85, 0x78}, {0x86, 0x8c}, {0x87, 0x9e},
    {0x88, 0xbb}, {0x89, 0xd2}, {0x8a, 0xe6},
    {0x6a, 0x41}, {0x66, 0x00},
    {0x3e, 0x00}, {0x3f, 0xa4}
};

DEFINE_MUTEX(regs_mutex);

static void __inline__ ov9650_poweron(void)
{
    s3c_gpio_cfgpin(S3C2410_GPG(12), S3C2410_GPIO_OUTPUT);
    gpio_set_value(S3C2410_GPG(12), 0);
    mdelay(20);
}

static void __inline__ ov9650_poweroff(void)
{
    s3c_gpio_cfgpin(S3C2410_GPG(12), S3C2410_GPIO_OUTPUT);
    gpio_set_value(S3C2410_GPG(12), 1);
    mdelay(20);
}

static int __inline__ ov9650_check(void)
{
    u32 mid;

    mid = sccb_read(OV9650_SCCB_ADDR, 0x1c) << 8;
    mid |= sccb_read(OV9650_SCCB_ADDR, 0x1d);
    printk("SCCB address 0x%02X, manufacture ID 0x%04X, expect 0x%04X\n", OV9650_SCCB_ADDR, mid, OV9650_MANUFACT_ID);

    return (mid == OV9650_MANUFACT_ID) ? 1 : 0;
}

static u32 __inline__ show_ov9650_product_id(void)
{
    u32 pid;
    pid = sccb_read(OV9650_SCCB_ADDR, 0x0a)<<8;
    pid |= sccb_read(OV9650_SCCB_ADDR, 0x0b);
    printk("SCCB address 0x%02X, product ID 0x%04X, expect 0x%04X\n", OV9650_SCCB_ADDR, pid, OV9650_PRODUCT_ID);
    return pid;
}

static void ov9650_init_regs(void)
{
    int i;

    down(&regs_mutex);
    for (i=0; i<ARRAY_SIZE(regs); i++)
    {
        if (regs[i].subaddr == 0xff)
        {
            mdelay(regs[i].value);
            continue;
        }
        sccb_write(OV9650_SCCB_ADDR, regs[i].subaddr, regs[i].value);
        int j;
#define WRITETIMES 1
        for(j = 0; j < WRITETIMES; j++) {
            sccb_write(OV9650_SCCB_ADDR, regs[i].subaddr, regs[i].value);
            if(regs[i].value == sccb_read(OV9650_SCCB_ADDR, regs[i].subaddr))
                break;
        }
    }

#if 1 /* This is the real function */
    /* read the regs value */
    int read_regs_value_counter;
    read_regs_value_counter  = 1;
    printk(KERN_ALERT"#### Now I'm checking the setted regs No.%d\n", read_regs_value_counter);
    for(i = 0; i < ARRAY_SIZE(regs); i++) {
        if(regs[i].subaddr == 0xff) {
            mdelay(regs[i].value);
            continue;
        }
        if(
                (regs[i].subaddr == 0x12 && regs[i].value == 0x80) ||
                (regs[i].subaddr == 0x13 && regs[i].value == 0xe5) ||
                (regs[i].subaddr == 0x3f && regs[i].value == 0xa6) ||
                (regs[i].subaddr == 0x3a && regs[i].value == 0x00) ||
                (regs[i].subaddr == 0x3e && regs[i].value == 0x02))
            continue;
        else {
            u8 RegAddr;
            u8 RegVal;

            RegAddr = regs[i].subaddr;
            RegVal = sccb_read(OV9650_SCCB_ADDR, RegAddr);
            if(!(regs[i].value == RegVal))
                printk(KERN_ALERT"Reg address 0x%02X, reading value is 0x%02X, expected 0x%02X\n", RegAddr, RegVal, regs[i].value);
        }
    }
    printk(KERN_ALERT"###########################################");
#endif
    up(&regs_mutex);
}

int s3c2440_ov9650_init(void)
{
    printk(KERN_ALERT"Loading OV9650 driver.........\n");
    /* power on. */
    ov9650_poweron();
    mdelay(100);

    /* check device. */
    if (ov9650_check() == 0 && ov9650_check() == 0)
    {
        printk(KERN_ERR"No OV9650 found!!!\n");
        return -ENODEV;
    }

    show_ov9650_product_id();
    ov9650_init_regs();
    printk("ov9650 init done!\n");
    return 0;
}
#if 0
module_init(ov9650_init);
module_exit(ov9650_cleanup);
MODULE_LICENSE("GPL");
#endif
